<template>
  <form-dialog
    :title="title"
    :visible="visible"
    @handleClose="handleClose"
  >
    <template #content>
      <div class="tip-container">
        <el-form ref="form" :model="formData" :rules="formRules" :label-width="language === 'zh' ? '100px' : '120px'" v-loading="loading">
          <el-form-item :label="$t('clientListManage.versionUpdate')" prop="name">
            <el-select v-model="formData.chooseVersion" :placeholder="$t('clientListManage.chooseVersion')">
              <el-option
                v-for="item in options"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <!-- <div class="message-container">
        <span>{{ message }}</span>
      </div> -->
    </template>
    <template #footer>
      <el-button @click="handleClose">{{ $t('common.cancel') }}</el-button>
      <el-button type="primary" @click="submit" :loading="loading">{{ $t('common.confirm') }}</el-button>
    </template>
  </form-dialog>
</template>
<script lang="ts">
import Vue from 'vue';
import FormDialog from '../../../components/Dialog/formDialog.vue';
import { AppModule } from '../../../store/modules/app';
import { AuthModule } from '../../../store/modules/auth';
export default Vue.extend({
  components: { FormDialog },
  props: {
    updateMethod: {
      type: Function,
      default() {
        return () => Promise.resolve();
      }
    }
  },
  data() {
    return {
      visible: false,
      loading: false,
      title: "",
      tip: "",
      message: "",
      versionList: '',
      formData: {
        chooseVersion: ''
      },
      formRules: {
      },
      options: [],
      param: {
        id: 'null',
        name: this.$t('clientListManage.isnull').toString()
      },
      rowData:'',
      language: AppModule.language,
    }
  },
  methods: {
    init({
      title,
      tip,
      message,
      versionList,
      type,
      row
    }: any) {
      (this as any).title = title;
      (this as any).tip = tip;
      (this as any).message = message;
      
      (this as any).versionList = JSON.parse(JSON.stringify(versionList));
      (this as any).rowData = JSON.parse(JSON.stringify(row));
      (this as any).formData.chooseVersion = (this as any).rowData.client_version_to_upgrade || "null";
      this.options = [];
      (this as any).param.name = (this as any).$t('clientListManage.isnull');
      (this as any).$pandaApi.apiGetClientVersions().then((res: any) => {
        this.options = [];
        for(let i in res as any) {
          if(res[i].product_model === type) {
            (this as any).options.push(res[i] as any)
          }
        }
        (this as any).options.push((this as any).param);
        (this as any).visible = true;
      });
    },
    handleClose() {
      (this as any).visible = false;
      (this as any).loading = false;
    },
    submit() {
      (this as any).loading = true;
      let params = {};
      if (this.formData.chooseVersion && this.formData.chooseVersion != "null") {
        params = {
          client_version_to_upgrade: (this.formData.chooseVersion)
        };
      } else {
        params = {
          client_version_to_upgrade: null
        };
      }
      
      (this as any).updateMethod((this as any).versionList, params).then(() => {
        (this as any).visible = false;
      }).finally(() => {
        (this as any).loading = false;
      });
    }
  }
})
</script>
<style lang="scss" scoped>
  .tip-container {
    padding-top: 20px;
    .icon {
      width: 80px;
      padding-right: 20px;
      display: inline-block;
      text-align: right;
      &::before {
        font-size: 20px;
        color:#fe983d;
      }
    }

    .text {
      color: #333333;
      font-size: 16px;
    }
  }
  .message-container {
    color: #999999;
    font-size: 12px;
    padding-left: 80px;
    margin: 10px 0px;
  }
</style>
